package com.baizhi.service.impl;

import com.baizhi.dao.CmfzGuruDao;
import com.baizhi.entity.CmfzGuru;
import com.baizhi.entity.CmfzUser;
import com.baizhi.service.GuruService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.List;

@Service
public class GuruServiceImpl implements GuruService {

    @Resource
    private CmfzGuruDao cmfzGuruDao;


    @Override
    public Page<CmfzGuru> selectAll(String name,Integer page, Integer limit) {
        //1.创建一个分页对象 封装分页数据
        Page<CmfzGuru> guruPage = new Page<>(page, limit);
        //2.使用分页查询的方法

        QueryWrapper<CmfzGuru> queryWrapper = new QueryWrapper<>();
        //2.将条件写入 条件构造器
        queryWrapper.like("guru_nickname",name);
        Page<CmfzGuru> cmfzGuruPage =null;
        if (name!=null){
            cmfzGuruPage = cmfzGuruDao.selectPage(guruPage, queryWrapper);
        }else {

            cmfzGuruPage = cmfzGuruDao.selectPage(guruPage, null);
        }
        return cmfzGuruPage;
    }

    @Override
    public CmfzGuru showOne(Integer id) {

        return cmfzGuruDao.selectById(id);
    }

    @Override
    public void insert(CmfzGuru cmfzGuru) {
        try {
            cmfzGuruDao.insert(cmfzGuru);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void delete(Integer id) {
        try {
            cmfzGuruDao.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void update(CmfzGuru cmfzGuru) {
        try {
            cmfzGuruDao.update(cmfzGuru,null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public HSSFWorkbook getGurusWorkbook() {
//        查询guru数据  生成Excel文件
        List<CmfzGuru> cmfzGurus = cmfzGuruDao.selectList(null);
        String[] titles={"上师编号","上师图片地址","上师名字","上师法号","上师状态"};

//        写出标题栏数据

        HSSFWorkbook workbook = null;
        try {
            workbook = new HSSFWorkbook();

            HSSFSheet sheet = workbook.createSheet();

            HSSFRow titleRow = sheet.createRow(0);

            for (int i = 0; i < titles.length; i++) {
                HSSFCell cell = titleRow.createCell(i);
                cell.setCellValue(titles[i]);
            }
//写入上师数据
            for (int i = 0; i <cmfzGurus.size() ; i++) {
                HSSFRow row= sheet.createRow(i + 1);

    //            写入上师数据

                CmfzGuru cmfzGuru = cmfzGurus.get(i);

                Class<? extends CmfzGuru> guruClass = cmfzGuru.getClass();

                Field[] fields = guruClass.getDeclaredFields();
                for (int j = 0; j < fields.length; j++) {
                    HSSFCell cell = row.createCell(j);
                    Field field = fields[j];

                    field.setAccessible(true);
                    Object o = field.get(cmfzGuru);
                    cell.setCellValue(o.toString());
                }

            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        return workbook;
    }

    @Override
    public void deleteAll(List<CmfzGuru> ids) {
        try {
            cmfzGuruDao.deleteBatchIds(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
